﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BanDTBLL;
using PagedList;
using System.IO;
namespace _1061102_1061114_1061129_1061132_1061141.Controllers
{ 
    public class TinTucController : Controller
    {
        private ban_dien_thoai_dbEntities db = new ban_dien_thoai_dbEntities();

        //
        // GET: /TinTuc/

        public ViewResult Index()
        {
            return View(db.tintuc.ToList());
        }
        public ViewResult W_Index(int? page)
        {
            var tin = from s in db.tintuc
                      select s;
            tin = tin.OrderByDescending(s => s.ngaydang);
            int pageSize = 3;
            int pageNumber = (page ?? 1);
            return View(tin.ToPagedList(pageNumber, pageSize));

            //return View(db.tintuc.ToList());
        }
        //
        // GET: /TinTuc/Details/5

        public ViewResult Details(int id, string content)
        {
            tintuc tintuc = db.tintuc.Single(t => t.ma == id);
            List<BL_TinTuc> dsBL = db.BL_TinTuc.Where(bl => bl.maTin == id).ToList();
            if (!string.IsNullOrEmpty(content))
            {
                BL_TinTuc bluan = new BL_TinTuc();
                bluan.maTin = id;
                bluan.noidung = content;
                bluan.ngaydang = DateTime.Now;
                db.BL_TinTuc.AddObject(bluan);
                db.SaveChanges();
            }

            return View(tintuc);
        }

        //
        // GET: /TinTuc/Create

        public ActionResult Create()
        {
            return View();
        }

        //)
        // POST: /TinTuc/Create

        [HttpPost]
        public ActionResult Create(tintuc tintuc, HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
            {
                // extract only the fielname
                var fileName = Path.GetFileName(file.FileName);
                // store the file inside ~/App_Data/uploads folder
                var path = Path.Combine(Server.MapPath("~/Content/themes/images"), fileName);
                tintuc.hinhanh = fileName;
                file.SaveAs(path);
            }
            else
                tintuc.hinhanh = "Phone-Icon.jpg";
            if (ModelState.IsValid)
            {
                tintuc.ngaydang = DateTime.Now;
                if (string.IsNullOrEmpty(tintuc.tieude))
                    tintuc.tieude = "[Chưa đặt tiêu đề]";
                db.tintuc.AddObject(tintuc);
                db.SaveChanges();
                return RedirectToAction("W_Index");
            }

            return View(tintuc);
        }

        //
        // GET: /TinTuc/Edit/5

        public ActionResult Edit(int id)
        {
            tintuc tintuc = db.tintuc.Single(t => t.ma == id);
            return View(tintuc);
        }

        //
        // POST: /TinTuc/Edit/5

        [HttpPost]
        public ActionResult Edit(tintuc tintuc, HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
            {
                // extract only the fielname
                var fileName = Path.GetFileName(file.FileName);
                var path = Path.Combine(Server.MapPath("~/Content/themes/images"), fileName);
                tintuc.hinhanh = fileName;
                file.SaveAs(path);
            }
            else
            {
                tintuc.hinhanh = "Phone-Icon.jpg";
            }
            if (ModelState.IsValid)
            {
                tintuc.ngaydang = DateTime.Now;
                if (string.IsNullOrEmpty(tintuc.tieude))
                    tintuc.tieude = "[Chưa đặt tiêu đề]";
                db.tintuc.Attach(tintuc);
                db.ObjectStateManager.ChangeObjectState(tintuc, EntityState.Modified);
                db.SaveChanges();
                return RedirectToActionPermanent("Details", "TinTuc", new { id = tintuc.ma });
            }
            return View(tintuc);
        }
        public ActionResult XoaBinhLuan(int id, int matin)
        {
            BL_TinTuc tintuc = db.BL_TinTuc.Single(t => t.maBL == id);
            db.BL_TinTuc.DeleteObject(tintuc);
            db.SaveChanges();
            return RedirectToActionPermanent("Details", "TinTuc", new { id = matin });
        }
        //
        // GET: /TinTuc/Delete/5

        public ActionResult Delete(int id)
        {
            tintuc tintuc = db.tintuc.Single(t => t.ma == id);
            db.tintuc.DeleteObject(tintuc);
            db.SaveChanges();
            return RedirectToAction("W_Index");
        }

        ////
        //// POST: /TinTuc/Delete/5

        //[HttpPost, ActionName("Delete")]
        //public ActionResult DeleteConfirmed(int id)
        //{            
        //    tintuc tintuc = db.tintuc.Single(t => t.ma == id);
        //    db.tintuc.DeleteObject(tintuc);
        //    db.SaveChanges();
        //    return RedirectToAction("W_Index");
        //}

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}